<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-04 12:54
 *
 * 项目：rm  -  $  - inputDataValidation.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\widgets\inputs;

use Lev;
use modules\levfm\table\form\FormData\fieldValidations;

!defined('INLEV') && exit('Access Denied LEV');


class inputDataValidation extends fieldValidations
{

    public static function errorInputtypeMessage($valiData, $inputInfo = [], $opInfo = []) {
        inputDataValidation::selectOption($valiData, $inputInfo);

        static::$errorMsg || parent::errorMessage($valiData, $inputInfo, $opInfo);

        static::$errorMsg && static::$errorMsg['ErrorInputName'] = $inputInfo['inputname'];
    }

    public static function selectOption($valiData, $inputInfo = []) {
        if ($valiData) {
            if ($inputInfo['inputtype'] == 'number' && !is_numeric($valiData)) {
                static::$errorMsg = Lev::responseMsg(-404003, '仅限输入数字');
                return;
            }
            if (inputsFormWidget::isSelectOnly($inputInfo)) {
                $options = inputsFormWidget::getSelectItem($inputInfo);
                if ($options) {
                    if (isset($options[$valiData])) {
                    } else {
                        if (stripos($inputInfo['inputtype'], 'selects') !== false) {
                            $arr = is_array($valiData) ? $valiData : json_decode($valiData, true);
                            if ($arr) {
                                foreach ($arr as $v) {
                                    if (!isset($options[$v])) {
                                        $arr = false;
                                        break;
                                    }
                                }
                            }
                        }
                        if (empty($arr)) {
                            static::$errorMsg = Lev::responseMsg(-404002, '不存在的选项值：' . $valiData);
                            return;
                        }
                    }
                }
            }
        }
    }

}